package com.ruoyi.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import static com.aliyun.teautil.Common.toJSONString;

/**
 * 阿里云发送短信服务
 */
public class ALiYunSms {
    private static String accessKeyId = "LTAI4FgiJvDZzPqVXgGdFVmx";
    private static String accessKeySecret = "9d7c4l7XKVVR7bEFvfKQYWZ1WHECgD";

    public static Client createClient() throws Exception {
        Config config = new Config()
                .setAccessKeyId(accessKeyId)
                .setAccessKeySecret(accessKeySecret);
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new Client(config);
    }

    /**
     * 发送短信
     *
     * @param telephone
     * @param name
     * @param method
     * @param number
     * @return
     * @throws Exception
     */
    public static String sendSms(String telephone, String name, String method, String number) throws Exception {
        Client client = createClient();
        SendSmsRequest request = new SendSmsRequest()
                .setPhoneNumbers(telephone)
                .setSignName("北京穆德装饰")
                .setTemplateCode("SMS_177245336")
                .setTemplateParam("{\"name\":\"" + name + "\",\"method\":\"" + method + "\",\"number\":\"" + number + "\"}");
        SendSmsResponse response = client.sendSms(request);
        JSONObject bodyJson = JSONObject.parseObject(toJSONString(response.getBody()));
        bodyJson.put("Code", bodyJson.get("code"));
        return bodyJson.toJSONString();
    }

    /**
     * 发送短信
     *
     * @param telephone
     * @param name
     * @return
     * @throws Exception
     */
    public static String sendSms(String telephone, String name) throws Exception {
        Client client = createClient();
        SendSmsRequest request = new SendSmsRequest()
                .setPhoneNumbers(telephone)
                .setSignName("北京穆德装饰")
                .setTemplateCode("SMS_214830289")
                .setTemplateParam("{\"name\":\"" + name + "\"}");
        SendSmsResponse response = client.sendSms(request);
        JSONObject bodyJson = JSONObject.parseObject(toJSONString(response.getBody()));
        bodyJson.put("Code", bodyJson.get("code"));
        return bodyJson.toJSONString();
    }

    /**
     * 发送验证码
     *
     * @param telephone
     * @param code
     * @return
     * @throws Exception
     */
    public static String sendSmsCode(String telephone, String code) throws Exception {
        Client client = createClient();
        SendSmsRequest request = new SendSmsRequest()
                .setPhoneNumbers(telephone)
                .setSignName("北京穆德装饰")
                .setTemplateCode("SMS_176938786")
                .setTemplateParam("{\"code\":\"" + code + "\"}");
        SendSmsResponse response = client.sendSms(request);
        JSONObject bodyJson = JSONObject.parseObject(toJSONString(response.getBody()));
        bodyJson.put("Code", bodyJson.get("code"));
        return bodyJson.toJSONString();
    }


    public static void main(String[] args) throws Exception {
        String sendSmsCode = sendSmsCode("18311489642", "zyt");
        System.out.println(sendSmsCode);
    }

}
